home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / dlfcn / dlfcn.c next >
C/C++ Source or Header  |  1997-07-20  |  2KB  |  110 lines

  1. /*
  2. ** ****************************************************************************
  3. ** Loading/Unloading of DLLs for OS/2
  4. ** (c) 1997, Klaus Gebhardt
  5. ** ****************************************************************************
  6. */
  7.  
  8. /*
  9. ** ****************************************************************************
  10. ** This was written for the OS/2 port of Octave, but it is not part of Octave!
  11. ** You can use the code UNMODIFIED. If you think changes are necessary,
  12. ** please send me a mail (gebhardt@crunch.ikp.physik.th-darmstadt.de).
  13. ** Thanks,
  14. **   Klaus Gebhardt
  15. ** ****************************************************************************
  16. */
  17.  
  18. #define INCL_DOSMODULEMGR
  19. #include <os2.h>
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24. #include "dlfcn.h"
  25.  
  26.  
  27. static UCHAR  errbuf[2*BUFSIZ];
  28. static APIRET errvalid;
  29.  
  30.  
  31. void *dlopen (const char *path, int mode)
  32. {
  33.   HMODULE hmod;
  34.   UCHAR   dll[BUFSIZ];
  35.   UCHAR   bad[BUFSIZ];
  36.  
  37.   const char *c;
  38.   char *d;
  39.  
  40.   c = path;  d = dll;
  41.   while (*c)
  42.     {
  43.       if ((*c) == '/')  (*d) = '\\';
  44.       else              (*d) = (*c);
  45.       c++;  d++;
  46.     }
  47.   (*d) = (*c);
  48.  
  49.   errvalid = DosLoadModule (bad, BUFSIZ - 1, dll, &hmod);
  50.  
  51.   if (errvalid)
  52.     {
  53.       sprintf (errbuf, "DosLoadModule failed (rc = %lu):\n%s\nbad: %s",
  54.            errvalid, dll, bad);
  55.       return NULL;
  56.     }
  57.  
  58.   return (void *) hmod;
  59. }
  60.  
  61.  
  62. void *dlsym (void *handle, const char *symbol)
  63. {
  64.   HMODULE hmod;
  65.   void    *function;
  66.  
  67.   hmod = (HMODULE) handle;
  68.  
  69.   errvalid = DosQueryProcAddr (hmod, 0, symbol, (PFN *) &function);
  70.  
  71.   if (errvalid)
  72.     {
  73.       sprintf (errbuf, "DosQueryProcAddr failed (rc = %lu):\n"
  74.            "Could not find address for %s",
  75.            errvalid, symbol);
  76.       return NULL;
  77.     }
  78.  
  79.   return function;
  80. }
  81.  
  82.  
  83. char *dlerror(void)
  84. {
  85.   if (errvalid)
  86.     {
  87.       errvalid = 0;
  88.       return errbuf;
  89.     }
  90.   return NULL;
  91. }
  92.  
  93.  
  94. int dlclose(void *handle)
  95. {
  96.   HMODULE hmod;
  97.  
  98.   hmod = (HMODULE) handle;
  99.  
  100.   errvalid = DosFreeModule (hmod);
  101.  
  102.   if (errvalid)
  103.     {
  104.       sprintf (errbuf, "DosFreeModule failed (rc = %lu)", errvalid);
  105.       return -1;
  106.     }
  107.  
  108.   return 0;
  109. }
  110.